import {ref,computed} from 'vue'
import request from '@/common/utils/request'
export const useTable = (url,size=10)=>{
    const list = ref([])
    let page = ref(1)
    let pageSize = ref(size)
    let total = ref(0)
    let loading = ref(false)


    let sizeOption = ['10','20','30']
    // size 在  sizeOption  中的位置 插入相应的位置中
    let index = sizeOption.indexOf((String(size)))
    if(index<0){
        sizeOption.push(String(size))
    }
    sizeOption.sort((a,b)=>a-b)
    let pageSizeOptions = ref(sizeOption)
    const pagination = computed(()=>{
        return {
            total:total.value,
            current: page.value,
            pageSize: pageSize.value,
            showSizeChanger: true,
            pageSizeOptions:pageSizeOptions.value
        }
    })
    const getList = (params)=>{
        return new Promise((resolve,reject)=>{
            if(params?.hideLoading){
                loading.value = false
                delete params.hideLoading
            }else{
                loading.value = true
            }
            request
            .get("/work_ai", url, {...params,page:page.value,pageSize:pageSize.value})
            .then((res) => {
              resolve(res)
              total.value = res?.total??0
              list.value  = res?.list??[]
              loading.value = false
            }).catch((err)=>{
                reject(err)
                loading.value = false
            })
        })   
    }
    return {
      page, pageSize, total, loading,list, pagination,getList
    }
}